package com.yipinhouse.reception.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.InputStream;

public class OssUtil {

	static String id = "LTAI5tMv43p4P9zCaEkvWTcY";
	static String secret = "M87xJ3QRE4VBjtUTYivJixj6UNkYDU";
	static String endPoint = "oss-cn-chengdu.aliyuncs.com";
	
	/**
	 * 上传
	 * @param bucketName  
	 * @param fileName
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static String uploadFile(String bucketName,String fileName,InputStream file) throws Exception {
		OSS ossClient = new OSSClientBuilder().build("http://"+endPoint, id, secret);
		try {
			ossClient.putObject(bucketName, fileName, file);
			return "http://"+bucketName+"."+endPoint+"/"+fileName;
		} finally {
			if(ossClient != null) ossClient.shutdown();
		}
	}
	
	/**
	 * 删除文件
	 * @param bucketName
	 * @param fileName
	 * @return
	 */
	public static boolean removeFile(String bucketName,String fileName) {
		OSS ossClient = new OSSClientBuilder().build("http://"+endPoint, id, secret);
		try {
			ossClient.deleteObject(bucketName, fileName);
			return true;
		} finally {
			if(ossClient != null) ossClient.shutdown();
		}
	}
}
